package equipments;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedList;
import java.util.List;

import systems.database.DataBaseHandler;
import systems.languages.MultiLanguages;
import tools.BonusAttribut;
import tools.Visitor;

public class Accessory implements Items {

  private int id = 0;
  private String name;
  private int level;
  private int nbBonusAttr;
  private boolean hasDivineSlot;

  private int magical_resist;
  private List<BonusAttribut> bonusList;

  public Accessory(){
    this.nbBonusAttr =0;
    this.setBonusList(new LinkedList<BonusAttribut>());
  }

  public Accessory(String name,Statement transmission)
  {
    this.name=name;
    this.nbBonusAttr=0;
    this.setBonusList(new LinkedList<BonusAttribut>());
    initItem(transmission);
  }

  @Override
  public void init(String id) {
    this.id = Integer.parseInt(id);
    initItem(DataBaseHandler.getStatement());
  }

  @Override
  public int getLevel() {
    return this.level;
  }

  @Override
  public String getName() {
    return this.name;
  }

  @Override
  public int getNbBonusAttr() {
    return this.nbBonusAttr;
  }


  @Override
  public boolean hasDivineSlot() {
    return this.hasDivineSlot;
  }

  @Override
  public void initItem(Statement transmission) {
    ResultSet resultat;
    this.setBonusList(new LinkedList<BonusAttribut>());
    try {
      //Starting with getting the name ...
      resultat= transmission.executeQuery("SELECT body FROM "
          +MultiLanguages.getProperties("database.Language.name")
          +" where oid = "+this.id);
      while(resultat.next())
        this.name=resultat.getString("body");

      //Recuperation des stats des initialisation
      resultat = transmission.executeQuery("Select * from items where id="+id);
      while(resultat.next())
      {
        int cpt=1;
        while(cpt<13)
        {
          if(resultat.getString("bonus_attr"+cpt)!=null)
          {
            this.getBonusList().add(new BonusAttribut(resultat.getString("bonus_attr"+cpt)));
            this.nbBonusAttr++;
          }
          cpt++;
        }
        this.hasDivineSlot=true;
        this.level = resultat.getInt("level");
        this.setMagical_resist(resultat.getInt("magical_resist"));
        /*   if(this.nbBonusAttr!=0)
	          for(int i=1;i<this.nbBonusAttr+1;i++)
	            this.getBonusList().add(new BonusAttribut(resultat.getString("bonus_attr"+i)));
         */
      }
    } catch (SQLException e) {
      e.printStackTrace();
    }
    //	System.out.println(this.toString());
  }

  public String toString()
  {
    String bonus="";
    for(int i=0;i<this.getBonusList().size();i++)
      bonus+="\nBonus "+i+": "+this.getBonusList().get(i).toString();

    return "Name :"+ this.name+
    "\nlevel: "+this.level+
    "\nmagical_resist: "+this.getMagical_resist()+
    //	"\nGodstone slot: "+this.hasDivineSlot+
    bonus;
  }

  public void setMagical_resist(int magical_resist) {
    this.magical_resist = magical_resist;
  }

  public int getMagical_resist() {
    return magical_resist;
  }

  public void setBonusList(List<BonusAttribut> bonusList) {
    this.bonusList = bonusList;
  }

  public List<BonusAttribut> getBonusList() {
    return bonusList;
  }


  @Override
  public void accept(Visitor visitor) {
    visitor.visit(this);
  }

  @Override
  public int getID() {
    return this.id;
  }

  @Override
  public int getNbManastone() {
    return 0;
  }

  @Override
  public void updateName() {
    ResultSet resultat;
    try {
      resultat= DataBaseHandler.getStatement().executeQuery("SELECT body FROM "
          +MultiLanguages.getProperties("database.Language.name")
          +" where oid = "+this.id);
      while(resultat.next())
        this.name=resultat.getString("body");  
    } catch (SQLException e) {
      e.printStackTrace();
    }
  }

@Override
public void acceptBonusRemoval(Visitor visitor) {
	visitor.bonusvisit(this);	
}
}
